import { Component, OnInit } from "@angular/core";
import { OutputdesignotherdtoService } from "./outputdesignotherdto.service";
import { ElementRef } from "@angular/core";

@Component({
  selector: "app-outputdesignotherdto",
  templateUrl: "./outputdesignotherdto.component.html",
  styleUrls: ["./outputdesignotherdto.component.css"],
})
export class OutputdesignotherdtoComponent implements OnInit {
  public filePath: string = ""; //保存文件名

  constructor(
    private service: OutputdesignotherdtoService,
    private el: ElementRef
  ) {}

  ngOnInit(): void {}

  upload(e): void {
    this.filePath = e.target.files[0];
  }

  doSubmit(): void {
    //let filePath = this.el.nativeElement.querySelector("#filePath").value;
    // const filePath =
    //   "C:\\shilin.du\\SJNK-損保\\Workspace\\antlr-target\\CapInterestPacket.java";
    let filePath = this.filePath;
    if (!filePath) {
      alert("ファイルパスを入力してください。");
    }

    const index = filePath.lastIndexOf("\\");
    const fileName = filePath.substring(index + 1, filePath.lastIndexOf("."));

    const suffix = filePath.substring(filePath.lastIndexOf("."));

    if (suffix === ".java") {
      this.service.updateHomeWork(filePath).subscribe((data) => {
        const link = document.createElement("a");
        const blob = new Blob([data.body], {
          type: "application/vnd.ms-excel",
        });
        link.setAttribute("href", window.URL.createObjectURL(blob));
        //link.setAttribute("download", new Date().getTime() + ".xls");
        link.setAttribute("download", fileName + "仕様書.xls");
        link.style.visibility = "hidden";
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        this.filePath = "";
      });
    } else {
      alert("種類が間違えています。");
    }
  }
}
